﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 材质分类服务
    /// </summary>
    public class MaterialCatalogService : IMaterialCatalogService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Material_Catalog model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().Identifier;
        }

        public bool Delete(List<string> identifiers)
        {
            return dal.Updateable<T_Material_Catalog>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => identifiers.Contains(p.Identifier)).ExecuteCommand() > 0;
        }

        public T_Material_Catalog GetById(string identifier)
        {
            return dal.Get<T_Material_Catalog>(identifier);
        }

        public List<T_Material_Catalog> GetList(string pIdentifier)
        {
            Expression<Func<T_Material_Catalog, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(pIdentifier))
            {
                whereExp = whereExp.Compose((a) => a.PID == pIdentifier, Expression.AndAlso);
            }
            return dal.Queryable<T_Material_Catalog>().Where(whereExp).ToList();
        }

        public PageData<T_Material_Catalog> Query(string pIdentifier, string identifier, string eName, string cName, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Material_Catalog, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(pIdentifier))
            {
                whereExp = whereExp.Compose((a) => a.PID == pIdentifier, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(identifier))
            {
                whereExp = whereExp.Compose((a) => a.Identifier.StartsWith(identifier), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(eName))
            {
                whereExp = whereExp.Compose((a) => a.EName.StartsWith(eName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(cName))
            {
                whereExp = whereExp.Compose((a) => a.CName.StartsWith(cName), Expression.AndAlso);
            }

            List<T_Material_Catalog> list = dal.Queryable<T_Material_Catalog>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Material_Catalog> { Data = list, Page = page };
        }

        public bool SaveOrUpdate(List<T_Material_Catalog> list)
        {
            return dal.Save(list).Count > 0;
        }

        public bool Update(T_Material_Catalog model)
        {
            return dal.Updateable<T_Material_Catalog>(p => new T_Material_Catalog()
            {
                Identifier = model.Identifier,
                EName = model.EName,
                CName = model.CName,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.Identifier == model.Identifier).ExecuteCommand() > 0;

        }
    }
}
